package samsung.sds.survey.surveyStatistics.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import samsung.sds.survey.surveyStatistics.model.QuestionEntity;
import samsung.sds.survey.surveyStatistics.model.SurveyEntity;
import samsung.sds.survey.surveyStatistics.service.SurveyResultStatisticsService;
import samsung.sds.system.utils.CommonTools;

/**
 * @author long1.huang
 * @createDate 2016-01-19 
 * 
 * 统计已结束问卷结果控制层
 */
@Controller
@RequestMapping("/survey/surveyStatistics/surveyResultStatistics")
public class SurveyResultStatisticsController {

	@Autowired
	private SurveyResultStatisticsService surveyResultStatisticsServiceImpl;
	
	/**
	 * 对传入的问卷id查询结果统计的方法
	 * @param survetyId 需传入的问卷id
	 * @return 要显示的页面字符串
	 * @throws IOException
	 */
	@RequestMapping(value="/survey" , method=RequestMethod.POST)
	public String viewInfo(ModelMap modelMap, String survetyId,String type)throws IOException {

		// 根据获取的surveyId查询出该survey
		SurveyEntity survey = surveyResultStatisticsServiceImpl.findEntityById(survetyId);

		List<QuestionEntity> questionList = surveyResultStatisticsServiceImpl.getNormalCountPercent(survey.getQuestion(), survey.getId());
		
		modelMap.addAttribute("list", questionList);		
		modelMap.addAttribute("survey", survey);
		modelMap.addAttribute("type", type);
		modelMap.addAttribute("isAuthor", survey.getCreateUser().equals(CommonTools.getSessionUserName())? "Y": "N");
		return "/survey/surveyStatistics/surveyResultStatistics";
	}
	
	@RequestMapping(value="/surveyTab1_sitemesh_frame" , method=RequestMethod.GET)
	public String viewInfoTab1(ModelMap modelMap, String type,HttpServletRequest request)throws IOException {

		// 根据获取的surveyId查询出该survey
		String surveyId = request.getParameter("survey_id");
		SurveyEntity survey = surveyResultStatisticsServiceImpl.findEntityById(surveyId);

		List<QuestionEntity> questionList = surveyResultStatisticsServiceImpl.getNormalCountPercent(survey.getQuestion(), survey.getId());
		
		modelMap.addAttribute("list", questionList);		
		modelMap.addAttribute("survey", survey);
		modelMap.addAttribute("type", type);
		modelMap.addAttribute("isAuthor", survey.getCreateUser().equals(CommonTools.getSessionUserName())? "Y": "N");
		return "/survey/surveyStatistics/surveyResultStatistics_Tab1";
	}
	

	
	@RequestMapping(value="/surveyTab2_sitemesh_frame" , method=RequestMethod.GET)
	public String viewInfoTab2(ModelMap modelMap, String type,HttpServletRequest request)throws IOException {
		// 根据获取的surveyId查询出该survey
		String surveyId = request.getParameter("survey_id");
		SurveyEntity survey = surveyResultStatisticsServiceImpl.findEntityById(surveyId);
		
		List<QuestionEntity> questionList = surveyResultStatisticsServiceImpl.getNormalCountPercent(survey.getQuestion(), survey.getId());
		
		modelMap.addAttribute("list", questionList);		
		modelMap.addAttribute("survey", survey);
		modelMap.addAttribute("type", type);
		modelMap.addAttribute("isAuthor", survey.getCreateUser().equals(CommonTools.getSessionUserName())? "Y": "N");
		return "/survey/surveyStatistics/surveyResultStatistics_Tab2";
	}

	 

}
